package main

import (
	"github.com/gin-gonic/gin"
	"redis/lib"
	"time"
)

func main() {
	c := gin.New()
	c.Use(func(c *gin.Context) {
		defer func() {
			if e := recover(); e != nil {
				c.AbortWithStatusJSON(400, gin.H{"message": e})
			}
		}()
		c.Next()
	})
	a := 0
	c.GET("/", func(context *gin.Context) {
		thislock := lib.NewLockerWithTTL("555", time.Second*3).Lock()
		defer func() {
			thislock.Unlock()
		}()
		if context.Query("t") != "" {
			time.Sleep(time.Second * 5)
		}
		a++
		context.JSON(200, gin.H{
			"message": a,
		})
	})
	c.Run(":8080")

}
